/*******************************************************************************
 *                                    ZLG
 *                         ----------------------------
 *                         innovating embedded platform
 *
 * Copyright (c) 2001-present Guangzhou ZHIYUAN Electronics Co., Ltd.
 * All rights reserved.
 *
 * Contact information:
 * web site:    https://www.zlg.cn
 *******************************************************************************/
#ifndef __HC32F4A0_PWC_H
#define __HC32F4A0_PWC_H

#ifdef __cplusplus
extern "C" {
#endif  /* __cplusplus*/
#include "common/hc32f4a0_common.h"
#include "core/include/hc32f4a0_regs_pwc.h"
#include <stdint.h>

/* \brief HC32F4A0 PWC 设置值*/
typedef enum {
    PWC_DISABLE = 0,
    PWC_ENABLE  = 1,
} pwc_state_t;

#define IS_PWC_FCG0_UNLOCKED()  ((HC32F4A0_PWC->FCG0PC & PWC_FCG0PC_PRT0) == PWC_FCG0PC_PRT0)

/**
 * \brief PWC_FCG0 寄存器上锁
 */
static inline void pwc_fcg0_lock(void) {
    WRITE_REG32(HC32F4A0_PWC->FCG0PC, 0xA5A50001UL);
}

/**
 * \brief 解锁 PWC_FCG0 寄存器
 */
static inline void pwc_fcg0_unlock(void) {
    WRITE_REG32(HC32F4A0_PWC->FCG0PC, 0xA5A50001UL);
}

/**
 * \brief PWC 寄存器上锁
 */
static inline void pwc_lock(uint16_t Module) {
    HC32F4A0_PWC->FPRC = (0xA500U | (uint16_t)((uint16_t)(~Module) & (HC32F4A0_PWC->FPRC)));
}

/**
 * \brief 解锁 PWC 寄存器
 */
static inline void pwc_unlock(uint16_t Module) {
    HC32F4A0_PWC->FPRC |= Module;
}

/**
 * \brief 设置 PWC FCG0 外设时钟
 *
 * \param[in] fcg0_periph 要操作的外设时钟
 * \param[in] state       打开/关闭
 *
 * \retval 成功返回0
 */
int pwc_fcg0_periph_clk_set(uint32_t fcg0_periph, pwc_state_t state);
/**
 * \brief 设置 PWC FCG1 外设时钟
 *
 * \param[in] fcg1_periph 要操作的外设时钟
 * \param[in] state       打开/关闭
 *
 * \retval 成功返回0
 */
int pwc_fcg1_periph_clk_set(uint32_t fcg1_periph, pwc_state_t state);
/**
 * \brief 设置 PWC FCG2 外设时钟
 *
 * \param[in] fcg2_periph 要操作的外设时钟
 * \param[in] state       打开/关闭
 *
 * \retval 成功返回0
 */
int pwc_fcg2_periph_clk_set(uint32_t fcg2_periph, pwc_state_t state);
/**
 * \brief 设置 PWC FCG3 外设时钟
 *
 * \param[in] fcg3_periph 要操作的外设时钟
 * \param[in] state       打开/关闭
 *
 * \retval 成功返回0
 */
int pwc_fcg3_periph_clk_set(uint32_t fcg3_periph, pwc_state_t state);
#ifdef __cplusplus
}
#endif  /* __cplusplus  */


#endif

